package pathways.SpeciesSpace;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.sql.ResultSet;
import java.sql.SQLException;

import pathways.Database.Implementation.CDatabase;
import pathways.util.Debug.CDebug;
import pathways.util.helper.CItem;

public class CSpace
{
  private CItem tissue = new CItem(0,"");
  private CItem organ = new CItem(0,""); 
  private CItem cell = new CItem(0,"");
  private CItem subCellularSub = new CItem(0,"");
  private CItem subCellularProd = new CItem(0,"");
  private int ID = 0;
  
  public CSpace()
  {
  }
  
  public CItem getTissue()
  {
    return tissue;
  }
  
  public void setTissue(CItem tissue)
  {
    this.tissue = tissue;
  }
  
  public CItem getOrgan()
  {
    return organ;  
  }
  
  public void setOrgan(CItem organ)
  {
    this.organ = organ;
  }
  
  public CItem getCell()
  {
    return cell; 
  }
  
  public void setCell(CItem cell)
  {
    this.cell = cell; 
  }
  
  public CItem getSubCellularSubstrate()
  {
    return subCellularSub;
  }
  
  public void setSubCellularSubstrate(CItem subCellSub)
  {
    this.subCellularSub = subCellSub;
  }
  
  public CItem getSubCellularProduct()
  {
    return subCellularProd; 
  }
  
  public void setSubCellularProduct(CItem subCellProd)
  {
    this.subCellularProd = subCellProd; 
  }
 
  public int getID()
  {
    return ID; 
  }
  
  public boolean read(int id, CDatabase db)
  {
    ResultSet rs = db.getSelect().getSpace(id);
    
    try {
      
      if (rs.next())
      {
        ID = rs.getInt(1);
        int tissueID = rs.getInt(2);
        int organID = rs.getInt(3);
        int cellID = rs.getInt(4);
        int subCellSubID = rs.getInt(5);
        int subCellProdID = rs.getInt(6);
          
        rs = db.getSelect().getTissue(tissueID);
        if (!readItem(rs,tissue))
           return false;
        
        rs = db.getSelect().getOrgan(organID);
        if (!readItem(rs,organ))
           return false;
        
        rs = db.getSelect().getCell(cellID);
        if (!readItem(rs,cell))
           return false;
        
        rs = db.getSelect().getSubCellSub(subCellSubID);
        if (!readItem(rs,subCellularSub))
           return false;
        
        rs = db.getSelect().getSubCellProd(subCellProdID);
        if (!readItem(rs,subCellularProd))
           return false;  
      }
      
    } catch (SQLException sqle) {
      CDebug.println(this,"read() Exception: " + sqle.toString());
      return false;
    }
    
    return true; 
  } 
  
  private boolean readItem(ResultSet rs, CItem item)
  {
    try {
     if (rs.next())
     {
       item.setID(rs.getInt(1));
       item.setValue(rs.getString(2));
     } 
     else
     {
       CDebug.println(this, "No data was found");
       return false; 
     }   
    } catch (SQLException sqle) {
      CDebug.println(this, "readItem() Exception: " + sqle.toString());
      return false;
    }
    
    return true;     
  }
  
  public boolean save(CDatabase db)
  {
    ID = db.getUpdate().newSpace(tissue.getID(), organ.getID(), cell.getID(), subCellularSub.getID(), subCellularProd.getID());
    
    if (ID > 0)
      return true;
    else
      return false; 
  }
}